package com.miage.itrading.metier;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.labels.XYToolTipGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import com.miage.itrading.entites.Cotation;

public class IndicateursSup extends IndicateurSurCours{

	public IndicateursSup() {
		super();
	}

	public IndicateursSup(String nomIndicateur, List<Float> valeurs,int[] periodes) {
		super();
		super.nomIndicateur = nomIndicateur;
		super.valeurs = valeurs;
		super.periodes = periodes;
	}


	public IndicateursSup(String nomIndicateur, List<Float> valeurs,int[] periodes, Cotation cotation) {
		super();
		super.nomIndicateur = nomIndicateur;
		super.valeurs = valeurs;
		super.periodes = periodes;
		super.cotation = cotation;
	}

	public XYItemRenderer getRenderer(){
		XYItemRenderer  newRenderer = new XYLineAndShapeRenderer(true, false);
		return newRenderer ;
	}

	public TimeSeriesCollection getDataset(){

		TimeSeriesCollection xydataset = new TimeSeriesCollection();
		TimeSeries series = new TimeSeries(nomIndicateur);
		for(int j = 0 ; j < valeurs.size(); ++j){
			series.add(new Day(cotation.getHistoriques().get(j+getPeriode()).getDate()),valeurs.get(j));
		}

		xydataset.addSeries(series);
		return xydataset;
	}

	private ChartPanel getGenericPanel(Dimension d){
		String nom = getNomIndicateur() +"(";
		for(int i = 0; i< periodes.length ; ++i){
			nom += periodes[i];
			if (i < periodes.length -1) nom += "," ;
		}
		nom += ")";
		JFreeChart chart = ChartFactory.createTimeSeriesChart(nom, "Date", "Valeur", getDataset(), true, false, false);
		XYPlot xyplot = chart.getXYPlot();
		XYItemRenderer  renderer = new XYLineAndShapeRenderer(true, false);

		renderer.setSeriesStroke(0, new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
		renderer.setSeriesPaint(0, Color.black);

		renderer.setSeriesPaint(0,  new Color(0x000000));

		xyplot.getRangeAxis().setAutoRange(false);
		xyplot.getDomainAxis().setAutoRange(false);

		xyplot.setRenderer(renderer);

		ChartPanel tmp = new ChartPanel(chart);
		tmp.setPreferredSize(d);
		tmp.setMouseZoomable(true);
		tmp.setBackground(Color.WHITE);

		return tmp;
	}

	private ChartPanel getMACDPanel(Dimension d){
		String nom = getNomIndicateur() +"(";
		for(int i = 0; i< periodes.length ; ++i){
			nom += periodes[i];
			if (i < periodes.length -1) nom += "," ;
		}
		nom += ")";
		JFreeChart chart = ChartFactory.createTimeSeriesChart(nom, "Date", "Valeur", getDataset(), true, false, false);
		XYPlot xyplot = chart.getXYPlot();
		XYToolTipGenerator tooltipGenerator= new StandardXYToolTipGenerator("Date : {1}," + nom + ": {2}", new SimpleDateFormat("MM/dd/yyyy"),new DecimalFormat("0.00")); 

		xyplot.getRangeAxis().setAutoRange(false);
		xyplot.getDomainAxis().setAutoRange(false);

		xyplot.setRenderer(0,new StandardXYItemRenderer(StandardXYItemRenderer.LINES,tooltipGenerator));
		TimeSeriesCollection xydataset = new TimeSeriesCollection();
		TimeSeries series = new TimeSeries(0);
		for(int j = 0 ; j < valeurs.size(); ++j){
			series.add(new Day(cotation.getHistoriques().get(j+getPeriode()).getDate()),0);
		}

		xydataset.addSeries(series);
		xyplot.setDataset(1,xydataset); 
		xyplot.mapDatasetToRangeAxis(1, 0); 
		xyplot.setRenderer(1,new StandardXYItemRenderer(StandardXYItemRenderer.DISCONTINUOUS_LINES,tooltipGenerator)); 
		xyplot.getRenderer().setSeriesPaint(0, Color.BLUE);
		xyplot.setBackgroundPaint(Color.BLACK);
		xyplot.setDomainGridlinesVisible(false);
		ChartPanel tmp = new ChartPanel(chart);
		tmp.setSize(d);
		tmp.setMouseZoomable(true);
		tmp.setBackground(Color.BLACK);

		return tmp;
	}


	private ChartPanel getROCPanel(Dimension d){
		JFreeChart chart = ChartFactory.createTimeSeriesChart(getNomIndicateur() + "(" +  getPeriode() + ")", "Date", "Valeur", getDataset(), true, false, false);
		XYPlot xyplot = chart.getXYPlot();
		XYItemRenderer  renderer = new XYLineAndShapeRenderer(true, false);

		renderer.setSeriesStroke(0, new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
	
		XYToolTipGenerator tooltipGenerator= new StandardXYToolTipGenerator("Date : {1}," + getNomIndicateur() + "(" +  getPeriode() + ")" + ": {2}", new SimpleDateFormat("MM/dd/yyyy"),new DecimalFormat("0.00")); 
		xyplot.setRenderer(0,new StandardXYItemRenderer(StandardXYItemRenderer.LINES,tooltipGenerator));
		
		xyplot.getRangeAxis().setAutoRange(true);
		xyplot.getDomainAxis().setAutoRange(true);

		xyplot.setRenderer(0,new StandardXYItemRenderer(StandardXYItemRenderer.LINES,tooltipGenerator));
		xyplot.getRenderer().setSeriesPaint(0, Color.BLUE);
		

		TimeSeriesCollection xydataset = new TimeSeriesCollection();
		TimeSeries series = new TimeSeries(0);
		for(int j = 0 ; j < valeurs.size(); ++j){
			series.add(new Day(cotation.getHistoriques().get(j+getPeriode()).getDate()),0);
		}
		
		series.setRangeDescription("%");
		xydataset.addSeries(series);
	
		xyplot.setRenderer(1,new StandardXYItemRenderer(StandardXYItemRenderer.DISCONTINUOUS_LINES)); 
		xyplot.setBackgroundPaint(Color.BLACK);
		xyplot.setDataset(1, xydataset); 
		

		renderer.setSeriesStroke(1, new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
		renderer.setSeriesPaint(1, Color.BLUE);

		xyplot.mapDatasetToRangeAxis(1, 0); 
		xyplot.setDomainGridlinesVisible(false);
		ChartPanel tmp = new ChartPanel(chart);
		tmp.setSize(d);
		tmp.setMouseZoomable(true);
		tmp.setBackground(Color.WHITE);

		return tmp;
	}

	private ChartPanel getMomentumPanel(Dimension d){
		JFreeChart chart = ChartFactory.createTimeSeriesChart(getNomIndicateur() + "(" +  getPeriode() + ")", "Date", "Valeur", getDataset(), true, false, false);
		XYPlot xyplot = chart.getXYPlot();
		XYItemRenderer  renderer = new XYLineAndShapeRenderer(true, false);

		renderer.setSeriesStroke(0, new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
		XYToolTipGenerator tooltipGenerator= new StandardXYToolTipGenerator("Date : {1}," + getNomIndicateur() + "(" +  getPeriode() + ")" + ": {2}", new SimpleDateFormat("MM/dd/yyyy"),new DecimalFormat("0.00")); 
		xyplot.setRenderer(0,new StandardXYItemRenderer(StandardXYItemRenderer.LINES,tooltipGenerator));
		xyplot.getRenderer().setSeriesPaint(0, Color.BLUE);

		xyplot.getRangeAxis().setAutoRange(true);
		xyplot.getDomainAxis().setAutoRange(true);

		xyplot.setRenderer(0,new StandardXYItemRenderer(StandardXYItemRenderer.LINES,tooltipGenerator));

		TimeSeriesCollection xydataset = new TimeSeriesCollection();
		TimeSeries series = new TimeSeries(0);
		for(int j = 0 ; j < valeurs.size(); ++j){
			series.add(new Day(cotation.getHistoriques().get(j+getPeriode()).getDate()),0);
		}

		xydataset.addSeries(series);

		xyplot.setDataset(1, xydataset); 

		renderer.setSeriesStroke(1, new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
		xyplot.setRenderer(1,new StandardXYItemRenderer(StandardXYItemRenderer.DISCONTINUOUS_LINES)); 
		xyplot.getRenderer().setSeriesPaint(0, Color.BLUE);
		xyplot.setBackgroundPaint(Color.BLACK);
		xyplot.mapDatasetToRangeAxis(1, 0); 

		ChartPanel tmp = new ChartPanel(chart);
		tmp.setSize(d);
		tmp.setMouseZoomable(true);
		tmp.setBackground(Color.WHITE);
		xyplot.setDomainGridlinesVisible(false);
		return tmp;
	}

	private ChartPanel getRSIPanel(Dimension d){

		JFreeChart chart = ChartFactory.createTimeSeriesChart(getNomIndicateur() + "(" +  getPeriode() + ")", "Date", "Valeur", getDataset(), true, false, false);
		XYPlot xyplot = chart.getXYPlot();
		XYItemRenderer  renderer = new XYLineAndShapeRenderer(true, false);

		renderer.setSeriesStroke(0, new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
		XYToolTipGenerator tooltipGenerator= new StandardXYToolTipGenerator("Date : {1}," + getNomIndicateur() + "(" +  getPeriode() + ")" + ": {2}", new SimpleDateFormat("MM/dd/yyyy"),new DecimalFormat("0.00")); 
		xyplot.setRenderer(0,new StandardXYItemRenderer(StandardXYItemRenderer.LINES,tooltipGenerator));
		xyplot.getRenderer().setSeriesPaint(0, Color.BLUE);

		xyplot.getRangeAxis().setAutoRange(false);
		xyplot.getDomainAxis().setAutoRange(false);

		xyplot.setRenderer(0,new StandardXYItemRenderer(StandardXYItemRenderer.LINES,tooltipGenerator));

		TimeSeriesCollection xydataset = new TimeSeriesCollection();
		TimeSeries series = new TimeSeries("Survente");
		for(int j = 0 ; j < valeurs.size(); ++j){
			series.add(new Day(cotation.getHistoriques().get(j+getPeriode()).getDate()),30);
		}

		xydataset.addSeries(series);

		xyplot.setDataset(1, xydataset); 
		renderer.setSeriesStroke(1, new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

		xyplot.setRenderer(1,new StandardXYItemRenderer(StandardXYItemRenderer.DISCONTINUOUS_LINES)); 
		xyplot.getRenderer().setSeriesPaint(0, Color.BLUE);
		xyplot.setBackgroundPaint(Color.BLACK);
		xyplot.mapDatasetToRangeAxis(1, 0); 

		TimeSeriesCollection xydataset2 = new TimeSeriesCollection();
		TimeSeries series2 = new TimeSeries("Surachat");
		for(int j = 0 ; j < valeurs.size(); ++j){
			series2.add(new Day(cotation.getHistoriques().get(j+getPeriode()).getDate()),70);
		}

		xydataset.addSeries(series2);

		xyplot.setDataset(2, xydataset2); 

		renderer.setSeriesStroke(2, new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

		xyplot.setRenderer(2,new StandardXYItemRenderer(StandardXYItemRenderer.DISCONTINUOUS_LINES)); 
		xyplot.setBackgroundPaint(Color.BLACK);
		xyplot.mapDatasetToRangeAxis(2, 0); 

		ChartPanel tmp = new ChartPanel(chart);
		tmp.setSize(d);
		tmp.setMouseZoomable(true);
		tmp.setBackground(Color.WHITE);
		xyplot.setDomainGridlinesVisible(false);
		return tmp;
	}

	public ChartPanel getPanel(Dimension d){
		if(getNomIndicateur().contains("MACD"))return getMACDPanel(d);
		else if(getNomIndicateur().contains("RSI"))return getRSIPanel(d);
		else if(getNomIndicateur().contains("Momentum"))return getMomentumPanel(d);
		else if(getNomIndicateur().contains("ROC"))return getROCPanel(d);
		else return getGenericPanel(d);
	}

	public String getNomIndicateur() {
		return nomIndicateur;
	}


	public void setNomIndicateur(String nomIndicateur) {
		this.nomIndicateur = nomIndicateur;
	}
	public List<Float> getValeurs() {
		return valeurs;
	}
	public void setValeurs(List<Float> valeurs) {
		this.valeurs = valeurs;
	}

	public int getPeriode() {
		return periodes[0];
	}

	public int[] getPeriodes() {
		return periodes;
	}

	public void setPeriodes(int[] periodes) {
		this.periodes = periodes;
	}

	public Cotation getCotation() {
		return cotation;
	}

	public void setCotation(Cotation cotation) {
		this.cotation = cotation;
	}


}
